import os
import math

import pygame

import globalvars
import selector

#INDOCK = 0
ATCREATION = 0
ALIVE = 1
SELECTED = 2
RUNNING = 3


class DefendingObject(pygame.sprite.Sprite):
    """Object that defends (controlled by player)"""
    def __init__(self):
        pygame.sprite.Sprite.__init__(self) # Initialize base class
        #Globals.ObjectsExists.add(self)
        spriteFile = os.path.join('data', self.spriteName)
        self.objectImage = pygame.image.load(spriteFile).convert_alpha()
        #self._state = state
        #if self.state == ATCREATION:
        self.generate_creation_image()
        #self.react = 0
        self.reloadCount = 0

    def get_image(self):
        #if self.state == INDOCK:
        #pass
        if self.state == ATCREATION:
            return self.creationImage
        elif self.state == ALIVE:
            return self.objectImage
        elif self.state == SELECTED:
            return self.objectImage
        elif self.state == RUNNING:
            return self.objectImage

    #_state = ATCREATION
    image = property(get_image, doc="Returns the image used to draw this sprite")
    #state = property(getState, doc="State of this instance")

    #def getState(self):
        #return self._state

    def create(self, position):
        """Makes this object alive"""
        globalvars.objects_selectable.add(self)
        globalvars.objects_player.add(self)
        self.rect = self.objectImage.get_rect()
        self.state = ALIVE
        self.rect.center = position
        globalvars.number_of_tuxs -= 1 # TODO add resources and pay them instead of number_of_tuxs

    def select(self):
        self.state = SELECTED
        self.selector = selector.Selector(self)
        self.update_selected(True)

    def unselect(self):
        #self.rect.move_ip(1,1)
        self.state = ALIVE
        self.selector.kill()
        self.selector = None
        self.update_alive()

    def update(self):
        #if self.state == INDOCK:
        #pass
        if self.state == ATCREATION:
            self.rect.center = pygame.mouse.get_pos()
        elif self.state == ALIVE:
            self.update_alive()
        elif self.state == SELECTED:
            self.update_selected(False)
        elif self.state == RUNNING:
            self.update_running()

    def update_alive(self):
        if pygame.sprite.spritecollideany(self, globalvars.objects_tropical):
            self.state = RUNNING
        if self.reloadCount == 0: #self.react > 5 and self.reloadCount == 0:
            #self.react = 0
            self.range_collision()
        elif self.reloadCount != 0:
            self.reloadCount -= 1
            #self.react += 1

    def update_selected(self, recentSelect):
        #if RecentSelect:
        	#self.rect.move_ip(-1,-1)
        #self.rect
        #self.SelectImage = pygame.Surface([self.rect.width + 2, self.rect.height + 2]).convert_alpha()
        #self.SelectImage.fill([0,0,0,0])
        #self.SelectRect = self.SelectImage.get_rect()
        #pygame.draw.rect(self.SelectImage, [0,0,0], self.SelectRect, 1)
        #dest = self.SelectRect.left+1,self.SelectRect.top+1
        #self.SelectImage.blit(self.ObjectImage, dest)
        self.update_alive()
        #self.SelectRect.center = self.rect.center
        #self.rect.topleft = self.rect.move(-1,-1).topleft

    def update_running(self):
        self.rect.move_ip(0, 3)
        if self.rect.top > globalvars.resolution[1] or self.rect.left > globalvars.resolution[0]:
            self.kill()

    def generate_creation_image(self):
        self.creationImage = pygame.Surface([self.shootingRange*2,self.shootingRange*2]).convert_alpha()
        self.creationImage.fill([0, 0, 0, 0])
        self.rect = self.creationImage.get_rect()
        pygame.draw.circle(self.creationImage, [0, 0, 0], [self.shootingRange,self.shootingRange], self.shootingRange, 1)
        dest = self.rect.center[0] - self.objectImage.get_rect().center[0], self.rect.center[1] - self.objectImage.get_rect().center[1]
        self.creationImage.blit(self.objectImage, dest)
        self.rect.center = pygame.mouse.get_pos()
        globalvars.objects_draw.add(self)
        self.state = ATCREATION

    def range_collision(self):
        center = self.rect.center
        for unit in globalvars.objects_tropical.sprites():
            dx = unit.rect.center[0] - center[0]
            dy = unit.rect.center[1] - center[1]
            d = math.sqrt(pow(dx, 2) + pow(dy, 2))
            if d < self.shootingRange and self.reloadCount == 0:
                projectile = self.ammunition(center, unit.rect.center)
                globalvars.objects_arctic_projectiles.add(projectile)
                globalvars.objects_draw.add(projectile)
                self.reloadCount = self.reloadTime

